const map = {
  __call_api: ({ api, params={} }) => {
    const { quietly = true } = params;

    delete params.quietly;

    post({ api, data: params, quietly });
  },
  __navigate_to: url => {
    goTo({ url });
  },
  __redirect_to: url => {
    goTo({url}, true);
  },
  __navigate_to_mini_program: options => {
    wx.hideLoading();
    wx.navigateToMiniProgram(options);
  },
};

const doWithMap = params => {
  Object.keys(params).map((key) => {
    if (key.startsWith('__') && map[key] !== undefined) {
      const rs = params[key];
      console.log(`接口控制执行[${key}]，参数：`, rs);

      // 如果是调用接口
      if(key === '__call_api'){
        rs.params.quietly = params.isHideLoading;
        map[key](rs);
      }
      else{
        if (typeof rs === true) {
          map[key]();
        } 
        else {
          // 默认将值传到调用去
          map[key](rs);
        }
      }

      delete params[key];
    }
  });
};

export default doWithMap;